package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Appmanager;
import com.ruoyi.system.service.IAppmanagerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Appinfo;
import com.ruoyi.system.service.IAppinfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目信息Controller
 * 
 * @author yaozh
 * @date 2024-03-04
 */
@RestController
@RequestMapping("/system/appinfo")
public class AppinfoController extends BaseController
{
    @Autowired
    private IAppinfoService appinfoService;

    @Autowired
    private IAppmanagerService appManagerService;

    /**
     * 查询项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:appinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(Appinfo appinfo,@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        if(appList == null || appList.size() == 0){
            return getDataTable(new ArrayList<>());
        }
        startPage();
        List<Appinfo> list = appinfoService.selectAppinfoList(appinfo,dbName);
        return getDataTable(list);
    }

    /**
     * 导出项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:appinfo:export')")
    @Log(title = "项目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Appinfo appinfo,@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        List<Appinfo> list = new ArrayList<>();
        if(appList != null && appList.size() > 0){
            list = appinfoService.selectAppinfoList(appinfo,dbName);
        }
        ExcelUtil<Appinfo> util = new ExcelUtil<Appinfo>(Appinfo.class);
        util.exportExcel(response, list, "项目信息数据");
    }

    /**
     * 获取项目信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:appinfo:query')")
    @GetMapping(value = "/{appid}")
    public AjaxResult getInfo(@PathVariable("appid") Long appid,@RequestParam String dbName)
    {
        return success(appinfoService.selectAppinfoByAppid(appid,dbName));
    }

    /**
     * 新增项目信息
     */
    @PreAuthorize("@ss.hasPermi('system:appinfo:add')")
    @Log(title = "项目信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Appinfo appinfo,@RequestParam String dbName)
    {
        String appName = appinfo.getAppname();
        if(!StringUtils.hasLength(appName)){
            return error("请选择项目数据库名称");
        }
        int row = appinfoService.insertAppinfo(appinfo,dbName);
        if(row == -2){
            return error("项目数据库不存在！");
        }else if(row == -3){
            return error("项目数据库重复，无需添加，只需修改");
        }else{
            return toAjax(row);
        }
    }

    /**
     * 修改项目信息
     */
    @PreAuthorize("@ss.hasPermi('system:appinfo:edit')")
    @Log(title = "项目信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Appinfo appinfo,@RequestParam String dbName)
    {
        return toAjax(appinfoService.updateAppinfo(appinfo,dbName));
    }

    /**
     * 删除项目信息
     */
    @PreAuthorize("@ss.hasPermi('system:appinfo:remove')")
    @Log(title = "项目信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{appids}")
    public AjaxResult remove(@PathVariable Long[] appids,@RequestParam String dbName)
    {
        return toAjax(appinfoService.deleteAppinfoByAppids(appids,dbName));
    }

    /**
     * 查询项目信息,通常是只有返回一个项目
     */
    @PreAuthorize("@ss.hasPermi('system:appinfo:query')")
    @GetMapping("/appmanager")
    public AjaxResult listAllAppmanager(@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        return success(appList);
    }
}
